﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccess;

namespace QuanLyKhachSan.TemplateReport{
    public partial class XtrpPhieuThuDV : DevExpress.XtraReports.UI.XtraReport
    {
        public XtrpPhieuThuDV(List<ChiTietHoaDonDichVu> db )
        {
            InitializeComponent();
            xrSoPhieu.Text = "C" + new Random().Next(100,1000);
            xrNgay.Text = DateTime.Now.ToShortDateString();
            DataSource = db;
            lblTenKhachHang.Text = db[0].HoaDonDichVu.KhachHang.HoTenKH;
            lblNgaySuDung.Text = db[0].HoaDonDichVu.NgaySuDung.ToShortDateString();
            lblNgayThanhToan.Text = db[0].HoaDonDichVu.NgayThanhToan.ToShortDateString();
            cTongTien.Text = db[0].HoaDonDichVu.ThanhTien.ToString();
            cGiamGia.Text = GetGiamGia(db).ToString();
            GanDuLieu();cPhaiTra.Text = Parse(cTongTien.Text, cGiamGia.Text).ToString();
        }

        private double Parse(string tongTien, String giamGia)
        {
            return Convert.ToDouble(tongTien) - Convert.ToDouble(giamGia);
        }

        private double GetGiamGia(IEnumerable<ChiTietHoaDonDichVu> db)
        {
            return (from ct in db where ct.DichVu.GiamGia > 0 let pt = (double) ct.DichVu.GiamGia/100 select pt*Convert.ToDouble(ct.DichVu.Gia)*Convert.ToDouble(ct.SoLuong)).Sum();
        }

        private void GanDuLieu()
        {
            colTenDV.DataBindings.Add("Text", DataSource, "DichVu.TenDichVu");
            colGia.DataBindings.Add("Text", DataSource, "DichVu.Gia");
            colSoLuong.DataBindings.Add("Text", DataSource, "SoLuong");
            colGiamGia.DataBindings.Add("Text", DataSource, "DichVu.GiamGia");
        }

        private void ReportHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            cTongTien.Text = cTongTien.Text + ".000";
            cGiamGia.Text = cGiamGia.Text + ".000";
            cPhaiTra.Text = cPhaiTra.Text + ".000";
        }}
}
